package com.itheima.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.StrUtil;
import com.itheima.common.TokenHolder;
import com.itheima.entity.dto.dish.DishAddDto;
import com.itheima.entity.dto.dish.DishAddFlDto;
import com.itheima.entity.dto.dish.DishUpDto;
import com.itheima.entity.dto.dish.DishUpFlDto;
import com.itheima.entity.pojo.Dish;
import com.itheima.entity.pojo.Flavor;
import com.itheima.entity.vo.dish.*;
import com.itheima.mapper.DishMapper;
import com.itheima.mapper.FlavorMapper;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author huangzelnag
 * @create 2022-05-09
 */
@Service
public class DishServiceImpl implements DishService {
   @Autowired
    DishMapper dishMapper;

   @Autowired
    FlavorMapper flavorMapper;



    /**
     * 同过分页获取列表
     *
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public DishPageVo getDishPage(String name, Integer page, Integer pageSize) {

        //1.创建前端返回对象，将值存入
        DishPageVo dishPageVo = new DishPageVo();

        dishPageVo.setCurrent(page);
        dishPageVo.setSize(pageSize);

        //2.获取总页数
        if(StrUtil.isNotEmpty(name)){
            name="%"+name+"%";
        }
        Integer integer = dishMapper.selectNameLikeCount(name);
        dishPageVo.setTotal(integer);

        //3.获取分页列表
        page=(page-1)*pageSize;
        List<DishPageLVo> dishs = dishMapper.selectPage(name, page, pageSize);

//        List<DishPageLVo> list =new ArrayList<>();
//
//        if(CollUtil.isEmpty(dishs)){
//            return null;
//        }
//
//        for (Dish dish : dishs) {
//            if(Objects.isNull(dish)){
//                continue;
//            }
//            DishPageLVo dishPageLVo = new DishPageLVo();
//
//            //4.转换成返回前端的对象
//            BeanUtils.copyProperties(dish, dishPageLVo);
//            list.add(dishPageLVo);
//        }

        //5.将集合存入dishPageVo对象
        dishPageVo.setRecords(dishs);

        return dishPageVo;
    }

    /**
     * 添加菜品
     *
     * @param dishAddDto
     * @return
     */
    @Override
    public Boolean addDish(DishAddDto dishAddDto) {

        String name = dishAddDto.getName();
        Dish dishName = dishMapper.selectByName(name);

        if(Objects.nonNull(dishName)){

            Integer integer = dishMapper.deleteForce(dishName.getId());
            if(integer==0){
             return false;
            }

        }

        //1.存入菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishAddDto,dish );
        //1.1 创建主键id
        String dishId= new Snowflake().nextIdStr();
        dish.setId(dishId);

        //1.2获取调用者id
        String userId = TokenHolder.getCurrentId();
        dish.setCreateUser(userId);
        dish.setUpdateUser(userId);

        //1.3获取创建时间
        dish.setCreateTime(new Date());
        dish.setUpdateTime(new Date());

        //2.存入
        dishMapper.insert(dish);

        //3.遍历口味集合存入
        List<DishAddFlDto> dishAddFlDtos = dishAddDto.getFlavors();
        if(CollUtil.isNotEmpty(dishAddFlDtos)){
            for (DishAddFlDto dishAddFlDto : dishAddFlDtos) {
                if(Objects.isNull(dishAddFlDto)){
                    continue;
                }
                Flavor flavor = new Flavor();
                BeanUtils.copyProperties(dishAddFlDto, flavor);

                //3.1获取创建时间
                String flavorId = new Snowflake().nextIdStr();
                flavor.setId(flavorId);
                flavor.setDishId(dishId);

                //3.2
                flavor.setCreateTime(new Date());
                flavor.setUpdateTime(new Date());

                //3.3获取调用者id
                flavor.setCreateUser(userId);
                flavor.setUpdateUser(userId);
                //3.4存入
                flavorMapper.insert(flavor);

            }



        }


        return true;
    }



    /**
     * 批量删除id
     *
     * @param ids
     * @return
     */
    @Override
    public Boolean delDishById(String[] ids) {


        return  dishMapper.deleteById(ids)>0;
    }




    /**
     * 根据id查询信息
     *
     * @param id
     * @return
     */
    @Override
    public DishByIdVo getDishById(String id) {
        DishByIdVo dishByIdVo = dishMapper.selectById(id);

        if(Objects.isNull(dishByIdVo)){
            return null;
        }

        String     dishId = dishByIdVo.getId();


        Flavor flavor = flavorMapper.selectByDishId(dishId);
        if(Objects.isNull(flavor)){
            return dishByIdVo;
        }


        List<Flavor> list = new ArrayList<>();
        list.add(flavor);


        dishByIdVo.setFlavors(list);





        return dishByIdVo;
    }

    /**
     * 修改菜品信息
     *
     * @param dishUpDto
     * @return
     */
    @Override
    public Boolean upDishInfo(DishUpDto dishUpDto) {

        //1.更新信息
        //1.1 转换pojo对象
        Dish dish = new Dish();

        BeanUtils.copyProperties(dishUpDto, dish);
        //1.2获取调用者id
        String userId = TokenHolder.getCurrentId();
        dish.setUpdateUser(userId);
        //1.3获取创建时间
        dish.setUpdateTime(new Date());

        //1.4  更新菜品表
        Boolean update = dishMapper.update(dish);
        //1.5 更新字段是否成功
        if(!update){
            return false;
        }

        //2.1 根据id删除口味表
        String id = dishUpDto.getId();
        flavorMapper.deleteByDishId(id);


        List<DishUpFlDto> flavors = dishUpDto.getFlavors();
        if(CollUtil.isEmpty(flavors)){
            return true;
        }


        //3.存放口味信息
        for (DishUpFlDto dishAddFlDto : flavors) {
            if(Objects.isNull(dishAddFlDto)){
                continue;
            }
            Flavor flavor = new Flavor();
            BeanUtils.copyProperties(dishAddFlDto, flavor);

            //3.1
            String flavorId = new Snowflake().nextIdStr();
            flavor.setId(flavorId);
            flavor.setDishId(dishUpDto.getId());

            //3.2获取创建时间
            flavor.setCreateTime(new Date());
            flavor.setUpdateTime(new Date());

            //3.3获取调用者id
            flavor.setCreateUser(userId);
            flavor.setUpdateUser(userId);
            //3.4存入口味表
            flavorMapper.insert(flavor);
        }



        return true;
    }

    /**
     * 菜品启用或者禁用
     *
     * @param status
     * @param ids
     * @return
     */
    @Override
    public Boolean upDishStatus(Integer status, String[] ids) {

          Boolean flag =  dishMapper.upState(status,ids);


        return flag;
    }

    /**
     * 通过分类id获取列表
     *
     * @param CategoryId
     * @return
     */
    @Override
    public  List<DishListByCategoryVo> getDishListByCategoryId(String CategoryId) {
        List<DishListByCategoryVo> dishListByCategoryVos = dishMapper.selectByCategoryId(CategoryId);

        if(CollUtil.isEmpty(dishListByCategoryVos)){
            return dishListByCategoryVos;
        }


        for (DishListByCategoryVo dishListByCategoryVo : dishListByCategoryVos) {
            if(Objects.isNull(dishListByCategoryVo)){
              continue;
            }
            String     dishId = dishListByCategoryVo.getId();
            Flavor flavor = flavorMapper.selectByDishId(dishId);
            if(Objects.isNull(flavor)){
                continue;
            }

            List<Flavor> list = new ArrayList<>();
            list.add(flavor);

            dishListByCategoryVo.setFlavors(list);

        }

        return dishListByCategoryVos;
    }

    /**
     * 获取菜单列表
     *
     * @param categoryId
     * @param status
     * @return
     */
    @Override
    public List<DishListVo> getDishList(String categoryId, String status) {

        List<DishListVo> list=  dishMapper.selectList(categoryId,status);

        for (DishListVo dishListVo : list) {
            String id = dishListVo.getId();
            List<Flavor> flavors = flavorMapper.selectByDishIdLa(id);

            dishListVo.setFlavors(flavors);

        }




        return list;
    }


}
